﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleAppAes
{
    class Program
    {
        static void Main(string[] args)
        {
            var content = "EOsNOQIHEdFBI9a/nZ7/0VN3g5z4d2vjYsN/nHYuE3ax8xI+WN1VL7t6ROFLSD22A4tEvqAEGknqQjEKlYAXjkr3VSB0txzr8xfhx5knmpvdjgB1t4UPCvKb+IMumbeE6/zZlYC/HIoMpjvFj2fpwnB3KNzMMLhajKxIaaAIoUjLA6/UPnerMR/cFI6fGGRcCt2Ykky7w4Y5Ejd/MycmoeRqyu45VGmbo8rasBrQ6ToRxKODIl9XTOOga4WNxR2lybHiaMPWYPcqGRNJ39iMvLb2Il029X1uI41mkyXEgRaDLIwktw8vaBg5rBHY5HP8+oXJ/90dXvtbXCH2o25ZPLN8uTEsLplRj4OpvwXpWoUG6d+5Xr3QQ/Zshmhr2aA41rsG76Bb5zjqcgUaELr2fV6G7kkz/g5D9ARz7hEh5ns6Az5G/+NVsMqhL0sb6+yxSIeU3kFzH6gdMjZ9Wd+fNR9eorARRyR4D26UwQQE0ikxkw7nGKWIKFGe1JJHOjN/XV9HtVXXU3tnz2O6zp78fN63mY4UGW8KYrH/WPBg7u4=";
            var res1 = AesDecrypt2("f5kly90zLBesPjT6Vxjr0A==", "guo+CDgs0E1SHUdepjaM/Q==", content);
            Console.WriteLine(res1);

            var input = string.Empty;
            do
            {
                Console.Write("input the key:");
                var key = Console.ReadLine();
                Console.Write("input the data:");
                var data = Console.ReadLine();

                var res = AesDecrypt(data, key);
                Console.WriteLine(res);

                Console.Write("Again?(Y/quit):");
                input = Console.ReadLine();

            } while (input != "quit");

        }

        /// <summary>
        ///  AES 解密
        /// </summary>
        /// <param name="str">明文（待解密）</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string AesDecrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = System.Security.Cryptography.CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

        public static string AesDecrypt2(string strKey, string strIv, string data)
        {
            var key = Convert.FromBase64String(strKey);
            var content = Convert.FromBase64String(data);
            var iv = Convert.FromBase64String(strIv);
            using (var aes = new RijndaelManaged())
            {
                aes.BlockSize = 128;
                aes.Key = key;
                aes.IV = iv;
                aes.Mode = CipherMode.CBC;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(content, 0, content.Length);
                        cs.Close();
                    }
                    var res = ms.ToArray();
                    return Encoding.UTF8.GetString(res);
                }
            }
        }
    }
}
